package com.xinpan.exercise;

public class MergeSortedArray {
    public void merge(int A[], int m, int B[], int n) {
        // Start typing your Java solution below
        // DO NOT write main() function
        int[] C = new int[m];
        
        for(int i = 0; i < m; i++)
        {
            C[i] = A[i];
        }
        int i = 0, j = 0;
        while(i < n && j < m)
        {
            if(B[i] < C[j])
            {
                A[i+j] = B[i++];
            }
            else
                A[i+j] = C[j++];
        }
        if(i == n)
        {
            for(;j < m; j++)
                A[i+j] = C[j];
        }
        else if(j == m)
        {
            for(;i< n; i++)
                A[i+j] = B[i];
        }

    }
}
